/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 文件通用类
 *
 * @author SoChishun
 * @version 1.0 2016-5-17
 * @see http://www.blogjava.net/youngturk/archive/2012/09/13/387635.html
 */
public class FileUtil {

    public static boolean createFile(String destFileName) {
        File file = new File(destFileName);
        if (file.exists()) {
            System.out.println("创建单个文件" + destFileName + "失败，目标文件已存在！");
            return false;
        }
        if (destFileName.endsWith(File.separator)) {
            System.out.println("创建单个文件" + destFileName + "失败，目标文件不能为目录！");
            return false;
        }
        //判断目标文件所在的目录是否存在
        if (!file.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            System.out.println("目标文件所在目录不存在，准备创建它！");
            if (!file.getParentFile().mkdirs()) {
                System.out.println("创建目标文件所在目录失败！");
                return false;
            }
        }
        //创建目标文件
        try {
            if (file.createNewFile()) {
                System.out.println("创建单个文件" + destFileName + "成功！");
                return true;
            } else {
                System.out.println("创建单个文件" + destFileName + "失败！");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建单个文件" + destFileName + "失败！" + e.getMessage());
            return false;
        }
    }

    public static boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            System.out.println("创建目录" + destDirName + "失败，目标目录已经存在");
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            System.out.println("创建目录" + destDirName + "成功！");
            return true;
        } else {
            System.out.println("创建目录" + destDirName + "失败！");
            return false;
        }
    }

    /**
     * 读取文件
     *
     * @param filename
     * @return 返回字符串内容或者null
     * @version 1.0 2016-5-16
     * @see http://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html
     */
    public static String readFile(String filename) {
        File file = new File(filename);
        if (!file.exists()) {
            return null;
        }
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String lineTxt = null;
            StringBuilder sbld = new StringBuilder();
            while ((lineTxt = br.readLine()) != null) {
                sbld.append(lineTxt);
            }
            return sbld.toString();
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 写入文件内容
     *
     * @param filename
     * @param content
     * @return
     * @version 1.0 2016-5-16
     * @see http://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html
     */
    public static boolean writeFile(String filename, String content) {
        File file = new File(filename);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file);
            try (BufferedWriter bufferWritter = new BufferedWriter(fw)) {
                bufferWritter.write(content);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
